package my.jwds.p796;

import java.util.ArrayList;
import java.util.List;

public class RotateString {

    public static void main(String[] args) {

        Solution1 obj = new Solution1();
        long start = System.currentTimeMillis();
        System.out.println(obj.rotateString("abcde", "cdeab"));
        System.out.println("运行时间:" + (System.currentTimeMillis() - start));
    }

    static class Solution {



        public boolean rotateString(String s, String goal) {
            for (int i = 0; i < s.length(); i++) {

                if ((s.substring(i)+s.substring(0,i)).equals(goal))return true;

            }
            return false;
        }

    }


    static class Solution1 {
        public boolean rotateString(String s, String goal) {
            if (s.length() != goal.length())return false;
            List<Integer>[][] lists = new ArrayList[26][];
            for (int i = 0; i < s.length(); i++) {
                int index1 = s.charAt(i) - 'a',index2=goal.charAt(i) - 'a';
                if (lists[index1] == null){
                    lists[index1] = new ArrayList[2];
                    lists[index1][0] = new ArrayList<>();
                    lists[index1][1] = new ArrayList<>();
                }
                lists[index1][0].add(i);
                if (lists[index2] == null){
                    lists[index2] = new ArrayList[2];
                    lists[index2][0] = new ArrayList<>();
                    lists[index2][1] = new ArrayList<>();
                }
                lists[index2][1].add(i);
            }
            int min = 101,index = 0;
            for (int i = 0; i < lists.length; i++) {
                if (lists[i] == null)continue;
                if (lists[i][0].size() != lists[i][1].size())return false;
                if (lists[i][0].size() < min){
                    index = i;
                    min = lists[i][0].size();
                }
            }

            for (int i = 0; i < min; i++) {
                String s1 = s.substring(lists[index][0].get(i)) + s.substring(0,lists[index][0].get(i));
                for (int j = 0; j < min; j++) {
                    if ((goal.substring(lists[index][1].get(j)) + goal.substring(0,lists[index][1].get(j))).equals(s1)){
                        return true;
                    }
                }
            }

            return false;
        }

    }

    static class Solution2 {



        public boolean rotateString(String s, String goal) {
            return s.length() == goal.length() && (s+s).contains(goal);
        }

    }


}